package com.bao.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: bao
 * Date: 2020/05/26 19:48
 * Description:
 * Version: V1.0
 */

@Controller
public class UploadController {
    @RequestMapping(value = "/upload.do")
    public String upload(MultipartFile file, String userName, HttpServletRequest request) throws IOException {
        System.out.println("用户名："+userName);
        String fileName = file.getOriginalFilename();//book.jpg
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String uploadPath = request.getServletContext().getRealPath("files") + File.separator + "upload" + File.separator;
        //判断上传文件类型
        if(suffix.equalsIgnoreCase(".jpg")){
            String uuid = UUID.randomUUID().toString();
            System.out.println(uploadPath);
            File newFile = new File(uploadPath + uuid+suffix);
            FileUtils.copyInputStreamToFile(file.getInputStream(),newFile);
            return "sucess";
        }else{
            return "error";
        }
    }
}
